<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  这个文件是 JCAT PHP框架的一部，该项目和此文件 均遵循 GNU 自由软件协议
// 
//  Copyleft 2008 JeCat.cn(http://team.JeCat.cn)
//
//
//  JCAT PHP框架 的正式全名是：Jellicle Cat PHP Framework。
//  “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》（《Prologue:Jellicle Songs for Jellicle Cats》）。
//  JCAT 是一个开源项目，它像音乐剧中的猫一样自由，你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
//  正在使用的这个版本是：0.4.0 /  SVN信息: $Id: class.JCAT_Request.php 1522 2009-03-23 10:10:08Z alee $
//
//
//
//  相关的链接：
//    [主页] http://jcat.JeCat.cn
//    [下载(HTTP)] http://code.google.com/p/jcat-php/downloads/list
//    [下载(svn)] svn checkout http://jcat-php.googlecode.com/svn/branches/0.4.0/Framework/ JCAT0.4
//    [在线文档] http://jcat.JeCat.cn/document
//    [社区] http://jj.jecat.cn/forum-7-1.html
//  不很相关：
//    [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
//    [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*-- Project Introduce --*/

class JCAT_Request
{
	const TYPE_CUS = 1 ;
	const TYPE_REQUEST = 2 ;
	const TYPE_GET = 4 ;
	const TYPE_POST = 8 ;
	const TYPE_FILES = 16 ;
	const TYPE_COOKIE = 32 ;
	const TYPE_CL = 64 ;
	const TYPE_SERVER = 128 ;
	
	const TYPE_DEFAULT = 243 ;	// TYPE_CUS|TYPE_REQUEST|TYPE_FILES|TYPE_COOKIE|TYPE_CL|TYPE_SERVER
	
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	JCAT_Request
	 */
	public function JCAT_Request($nRequestType=self::TYPE_DEFAULT,$sCLPPClassName=null)
	{
		$this->SetRequestType($nRequestType) ;
		
		// parse params from cl
		if( !self::IsHTTPRequest() )
		{
			if( !$sCLPPClassName )
			{
				$sCLPPClassName = self::$sDefaultCLPP ;
			}
			JCAT_ASSERT::ASSERT_ISCLASS($sCLPPClassName,'JCAT_ICommandLineParamParser',JCAT_Language::SentenceEx('参数 $sCLPPClassName(%s) 必须实现 JCAT_ICommandLineParamParser 接口。','JCAT',null));
			
			$aCLParamParser = new $sCLPPClassName() ;
			
			$arrArgvs = self::GetCommandLineParams() ;
			$this->arrCLParams = $aCLParamParser->Parse($arrArgvs) ;			
		}
		
		$this->arrParamSources = array(
			self::TYPE_CUS => &$this->arrCusParams
			, self::TYPE_REQUEST => &$_REQUEST
			, self::TYPE_GET => &$_GET
			, self::TYPE_POST => &$_POST
			, self::TYPE_FILES => &$_FILES
			, self::TYPE_COOKIE => &$_COOKIE
			, self::TYPE_CL => &$this->arrCLParams
			, self::TYPE_SERVER => &$_SERVER			
		) ;
		
		// 
		$this->SetParam('FULL_URL',
				'http://'.$this->GetParam('HTTP_HOST').$this->GetParam('REQUEST_URI')
				);
		
	}
	
	/**
	 * 设置对象属性 nRequestType
	 *
	 * @access	public
	 * @param	$nRequestType		int	nRequestType
	 * @return	void
	 */
	public function SetRequestType($nRequestType=self::TYPE_DEFAULT)
	{
		JCAT_ASSERT::ASSERT_INT($nRequestType) ;
		$this->nRequestType = $nRequestType ;
	}
	
	/**
	 * 取得对象属性 nRequestType
	 *
	 * @access	public
	 * @return	int
	 */
	public function GetRequestType()
	{
		return $this->nRequestType ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @static
	 * @return	JCAT_Request
	 */
	static public function GetGlobalInstance()
	{
		if( !self::$aGlobalInstance )
		{
			self::$aGlobalInstance = new JCAT_Request(self::TYPE_DEFAULT,self::$sDefaultCLPP) ;
		}
		return self::$aGlobalInstance ;
	}
	
	/**
	 * get user quest param
	 *
	 * @access	public
	 * @param	$sParamName		string
	 * @return	string
	 */
	public function GetParam($sParamName)
	{
		foreach ($this->arrParamSources as $nType=>&$arrParamSource)
		{
			if( !($this->nRequestType&$nType) )
			{
				continue ;
			}
			
			if( array_key_exists($sParamName,$arrParamSource) )
			{
				return $arrParamSource[$sParamName] ;
			}
		}

		return null ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function SetParam($sParamName,$Data)
	{
		$this->arrCusParams[$sParamName] = $Data ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @static
	 * @return	bool
	 */
	static public function IsHTTPRequest()
	{
		return !empty($_SERVER['HTTP_HOST']) ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @param	$sParamName='controller'		string
	 * @return	string
	 */
	public function GetRequestController($sParamName='controller')
	{
		$sControllerName = $this->GetParam($sParamName) ;
		
		if( !$sControllerName and defined($sControllerName) )
		{
			JCAT_ASSERT::ASSERT_ISCLASS(self::$sDefaultControllerClass,array('JCAT_IController')
						, "JCAT_Request::\$sDefaultControllerClass(%s) 必须是一个 JCAT_IController 类名。");
			
			$sControllerName = self::$sDefaultControllerClass ;
		}
		
		return $sControllerName ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	JCAT_IIterator
	 */
	public function CreateParamIterator()
	{
		$arrParamNames = array() ;
		foreach ($this->arrParamSources as $nType=>&$arrParamSource)
		{
			if( !($this->nRequestType&$nType) )
			{
				continue ;
			}
			
			$arrParamNames = array_merge($arrParamNames,array_keys($arrParamSource)) ;
		}
		
		return new JCAT_ArrayIterator( array_unique($arrParamNames) ) ;
	}

	/**
	 * Description
	 *
	 * @access	public
	 * @static
	 * @return	array
	 */
	static public function GetCommandLineParams()
	{
		global $argv ;
		$arrArgvs = is_array($argv)?$argv: array() ;
		array_shift($arrArgvs) ;
		
		return array_values($arrArgvs) ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	string
	 */
	public function MakeUrlQuery()
	{
		$arrParams = array() ;
		foreach ($this->arrParamSources as $nType=>&$arrParamSource)
		{
			if( !($this->nRequestType&$nType) )
			{
				continue ;
			}
			
			$arrParams = array_merge($arrParams,$arrParamSource) ;
		}
		
		return JCAT_Global::Array2UrlQeruy($arrParams) ;
	}
	
	
	/**
	 * 对象属性 nRequestType
	 * 
	 * @access	private
	 * @var		int
	 */
	private $nRequestType = self::TYPE_DEFAULT ;
	
	/**
	 * Description
	 * 
	 * @access	public
	 * @static
	 * @var		string
	 */
	static public $sDefaultCLPP = 'JCAT_CLPP_NamedParamStyle' ;
	
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @static
	 * @var		JCAT_ResultDispatcher
	 */
	static private $aGlobalInstance ;
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrCLParams = array() ;
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrCusParams = array() ;
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrParamSources ;
	
	/**
	 * Description
	 * @static
	 * @access	public
	 * @var		string
	 */
	static public $sDefaultControllerClass = null ;
}

?>